<template>
  <div class="app-container">
    <el-form ref="queryForm" inline :model="queryParams">
      <!-- <el-form-item prop="appName">
        <el-input
          v-model="queryParams.appName"
          clearable
          placeholder="搜索应用名称"
        />
      </el-form-item> -->
      <el-form-item prop="userId">
        <el-input
          v-model="queryParams.userId"
          clearable
          placeholder="搜索用户标识"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery"> 查询 </el-button>
        <el-button type="primary" @click="resetQuery"> 重置 </el-button>
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">
          新增
        </el-button>
      </el-form-item>
      <div
        style="
          display: inline;
          font-size: larger;
          padding-left: 1000px;
          text-align: center;
          margin: 0 auto;
          color: #fff;
        "
      >
        规则总数:{{ ruleTotal }}
      </div>
    </el-form>

    <el-table v-loading="loading" :data="pageList" fit stripe height="615px">
      <el-table-column
        label="序号"
        type="index"
        width="80"
        align="left"
      ></el-table-column>
      <el-table-column
        label="应用名称"
        prop="appName"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column
        label="应用唯一标识"
        prop="appId"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column
        label="设备标识"
        prop="deviceId"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column
        label="用户标识"
        prop="userId"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column
        label="设备备注"
        prop="memo"
        show-overflow-tooltip
        align="left"
      ></el-table-column>
      <el-table-column label="操作" align="left" width="200">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['/eva/v1/app/update']"
            >修改</el-button
          >
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['/eva/v1/app/delete']"
            >删除</el-button
          >
          <el-button
            type="text"
            @click="handleSetRulesInfp(scope.row)"
            v-hasPermi="['/eva/v1/rule/create']"
            >规则信息维护</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <el-dialog
      append-to-body
      center
      width="800px"
      :visible.sync="visible"
      :title="title"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="应用名称" prop="appName">
          <el-input
            v-model="form.appName"
            clearable
            placeholder="填写应用名称"
          />
        </el-form-item>
        <el-form-item label="应用唯一标识" prop="appId">
          <el-input
            v-model="form.appId"
            clearable
            :disabled="isEdit"
            placeholder="填写应用唯一标识"
          />
        </el-form-item>
        <el-form-item label="设备标识" prop="deviceId">
          <el-input
            v-model="form.deviceId"
            placeholder="设备标识"
            clearable
            :disabled="isEdit"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户标识" prop="userId">
          <el-input
            v-model="form.userId"
            placeholder="用户标识"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="设备备注" prop="memo">
          <el-input
            v-model="form.memo"
            type="textarea"
            rows="3"
            placeholder="设备备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="btnLoading" @click="submitForm">
          确 定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAppList, createApp, updateApp, deleteApp } from "@/api/app";

export default {
  name: "Trustrules",
  data() {
    return {
      queryParams: {
        page: 1,
        size: 10,
        appName: undefined,
        userId: undefined,
      },
      isEdit: false,
      loading: false,
      pageList: [],
      total: 0,
      ruleTotal: 0,
      visible: false,
      title: "新增",
      form: {},
      rules: {
        appName: { required: true, message: "请填写", trigger: "blur" },
        appId: { required: true, message: "请填写", trigger: "blur" },
        deviceId: { required: true, message: "请填写", trigger: "blur" },
        userId: { required: true, message: "请填写", trigger: "blur" },
      },
      btnLoading: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    handleSetRulesInfp(row) {
      this.$router.push({
        path: "/rulesinfo",
        query: {
          appId: row.appId,
          deviceId: row.deviceId,
          userId: row.userId,
        },
      });
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.btnLoading = true;

          if (this.form.id) {
            updateApp(this.form)
              .then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: "操作成功",
                  });
                  this.getList();
                  this.cancel();
                } else {
                  this.$message({
                    type: "error",
                    message: res.msg,
                  });
                }
              })
              .finally(() => (this.btnLoading = false));
          } else {
            // 新增
            createApp(this.form)
              .then((res) => {
                if (res.code == 0) {
                  this.$message({
                    type: "success",
                    message: "操作成功",
                  });
                  this.getList();
                  this.cancel();
                } else {
                  this.$message({
                    type: "error",
                    message: res.msg,
                  });
                }
              })
              .finally(() => (this.btnLoading = false));
          }
        }
      });
    },
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getAppList(this.queryParams)
        .then((res) => {
          this.pageList = res.data.items || [];
          this.total = res.data.total || 0;
          this.ruleTotal = res.data.ruleTotal || 0;
        })
        .finally(() => (this.loading = false));
    },
    handleAdd() {
      this.form = {};
      this.resetForm("form");
      this.title = "新增";
      this.visible = true;
      this.isEdit = false;
    },
    handleUpdate(row) {
      this.form = { ...row };
      this.title = "编辑";
      this.visible = true;
      this.isEdit = true;
      this.resetForm("form");
    },
    handleDelete(row) {
      this.$confirm("确定要删除此应用白名单吗？", "提示", {
        type: "warning",
      }).then(() => {
        deleteApp({ id: row.id }).then((res) => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: "操作成功",
            });
            this.getList();
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        });
      });
    },
    cancel() {
      this.form = {};
      this.resetForm("form");
      this.visible = false;
    },
  },
};
</script>

<style></style>
